Attribution.toJSON   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
var GedcomX = require('../'),
2
    utils = require('../utils');
3
4
/**
5
 * Define who is contributing information, when they contributed it, 
6
 * and why they are making the contribution.
7
 * 
8
 * @see {@link https://github.com/FamilySearch/gedcomx/blob/master/specifications/json-format-specification.md#attribution|GEDCOM X JSON Spec}
9
 * 
10
 * @class
11
 * @extends ExtensibleData
12
 * @param {Object} [json]
13
 */
14
var Attribution = function(json){
15
  
16
  // Protect against forgetting the new keyword when calling the constructor
17
  if(!(this instanceof Attribution)){
18
    return new Attribution(json);
19
  }
20
  
21
  // If the given object is already an instance then just return it. DON'T copy it.
22
  if(Attribution.isInstance(json)){
23
    return json;
24
  }
25
  
26
  this.init(json);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
27
};
28
29
Attribution.prototype = Object.create(GedcomX.ExtensibleData.prototype);
30
31
Attribution._gedxClass = Attribution.prototype._gedxClass = 'GedcomX.Attribution';
32
33
Attribution.jsonProps = [
34
  'changeMessage',
35
  'contributor',
36
  'created',
37
  'creator',
38
  'modified'
39
];
40
41
/**
42
 * Check whether the given object is an instance of this class.
43
 * 
44
 * @param {Object} obj
45
 * @returns {Boolean}
46
 */
47
Attribution.isInstance = function(obj){
48
  return utils.isInstance(obj, this._gedxClass);
49
};
50
51
/**
52
 * Initialize from JSON
53
 * 
54
 * @param {Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
55
 * @return {Attribution} this
56
 */
57
Attribution.prototype.init = function(json){
58
  
59
  GedcomX.ExtensibleData.prototype.init.call(this, json);
60
  
61
  if(json){
62
    this.setChangeMessage(json.changeMessage);
63
    this.setContributor(json.contributor);
64
    this.setCreated(json.created);
65
    this.setCreator(json.creator);
66
    this.setModified(json.modified);
67
  }
68
  return this;
69
};
70
71
/**
72
 * Get the change message.
73
 * 
74
 * @returns {String} Change message
75
 */
76
Attribution.prototype.getChangeMessage = function(){
77
  return this.changeMessage;
78
};
79
80
/**
81
 * Set the change message.
82
 * 
83
 * @param {String} changeMessage
84
 * @returns {Attribution} This object
85
 */
86
Attribution.prototype.setChangeMessage = function(changeMessage){
87
  this.changeMessage = changeMessage;
88
  return this;
89
};
90
91
/**
92
 * Get the contributor.
93
 * 
94
 * @returns {ResourceReference} contributor
95
 */
96
Attribution.prototype.getContributor = function(){
97
  return this.contributor;
98
};
99
100
/**
101
 * Set the contributor
102
 * 
103
 * @param {Object|ResourceReference} contributor ResourceReference representing the contributor.
104
 * @returns {Attribution} This instance
105
 */
106
Attribution.prototype.setContributor = function(contributor){
107
  if(contributor){
108
    this.contributor = GedcomX.ResourceReference(contributor);
109
  }
110
  return this;
111
};
112
113
/**
114
 * Get the created timestamp
115
 * 
116
 * @returns {Date} created
117
 */
118
Attribution.prototype.getCreated = function(){
119
  return this.created;
120
};
121
122
/**
123
 * Set the created timestamp
124
 * 
125
 * @param {Date|Number} date Integer timestamp (milliseconds since epoch) or JavaScript Date instance
126
 * @returns {Attribution} This instance
127
 */
128
Attribution.prototype.setCreated = function(date){
129
  if(date){
130
    this.created = new Date(date);
131
  }
132
  return this;
133
};
134
135
/**
136
 * Get the creator
137
 * 
138
 * @returns {ResourceReference} Creator
139
 */
140
Attribution.prototype.getCreator = function(){
141
  return this.creator;
142
};
143
144
/**
145
 * Set the creator
146
 * 
147
 * @param {Object|ResourceReference} creator ResourceReference representing the creator.
148
 * @returns {Attribution} This instance
149
 */
150
Attribution.prototype.setCreator = function(creator){
151
  if(creator){
152
    this.creator = new GedcomX.ResourceReference(creator);
153
  }
154
  return this;
155
};
156
157
/**
158
 * Get the modified timestamp
159
 * 
160
 * @returns {Date} modified
161
 */
162
Attribution.prototype.getModified = function(){
163
  return this.modified;
164
};
165
166
/**
167
 * Set the modified timestamp
168
 * 
169
 * @param {Date|Number} date Integer timestamp (milliseconds since epoch) or JavaScript Date instance
170
 * @returns {Attribution} This instance
171
 */
172
Attribution.prototype.setModified = function(date){
173
  if(date){
174
    this.modified = new Date(date);
175
  }
176
  return this;
177
};
178
179
/**
180
 * Export the object as JSON
181
 * 
182
 * @return {Object} JSON object
183
 */
184
Attribution.prototype.toJSON = function(){
185
  return this._toJSON(GedcomX.ExtensibleData, Attribution.jsonProps);
186
};
187
188
module.exports = Attribution;